<template>
  <div>
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <!--                    饼形图-->
          <div id="main2" style="height: 400px;width: 100%;"></div>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <!--                    柱状图-->
          <div id="main1" style="height: 400px;width: 100%;"></div>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>操作日志</span>
          </div>
          <div class="block">
            <el-timeline>
              <el-timeline-item v-for="item in logData">
                <el-card>
                  <h4>方法【{{ item.method }}】路径【{{ item.path }}】地址【{{ item.addr }}】</h4>
                  <p>{{ item.user.username }} 提交于 {{ item.create_time }} 响应耗时【{{ item.expend_time }}s】</p>
                </el-card>
              </el-timeline-item>
            </el-timeline>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import {api} from "@/assets/static/js/settings";

export default {
  name: "Index",
  data() {
    return {
      chartInstance: null,
      option_1: {
        title: {
          text: '访问统计'
        },
        tooltip: {},
        legend: {
          data: ['访问量']
        },
        xAxis: {
          data: []
        },
        yAxis: {},
        series: [{
          name: '每日请求次数',
          type: 'bar',
          data: []
        }]
      },
      option_2: {
        title: {
          text: '用户访问来源',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left',
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: '50%',
            data: [],
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      },
      logData: []
    }
  },
  mounted() {
  },
  created() {
    this.init_echarts_bar('main1', api.echarts_date, this.option_1)
    this.init_echarts_pie('main2', api.echarts_ua, this.option_2)
    this.get_log_list()
  },
  methods: {
    init_echarts_bar(eid, url, options) {
      this.$axios.get(url).then(res => {
        if (res) {
          // x轴 标签
          this.$set(options.xAxis, 'data', res.date)
          // 柱形数据
          this.$set(options.series[0], 'data', res.date_count)
          let instance = echarts.init(document.getElementById(eid));
          instance.setOption(options);
        }
      }).catch(error => {
        console.log(error)
        this.$message.error(error)
      })
    },
    init_echarts_pie(eid, url, options) {
      this.$axios.get(url).then(res => {
        if (res) {
          this.$set(options.series[0], 'data', res.results)
          let instance = echarts.init(document.getElementById(eid));
          instance.setOption(options);
        }
      }).catch(error => {
        console.log(error)
        this.$message.error(error)
      })
    },
    get_log_list() {
      this.$axios.get(api.log).then(res => {
        if (res) {
          this.logData = res.results.results.slice(0, 10)
        }
      })
    }
  }
}
</script>

<style scoped>
.el-row {
  margin: 50px;
}
</style>